<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
  <style>
    .done span {
      text-decoration: line-through;
      color: #ccc
    }
  </style>
</head>
<body>
  <div id="app">
    <h1>App</h1>
    <div>
      <input type="text" v-model="newVal">
      <button @click="add">添加新任务</button>
    </div>
    <hr />
    <ul>
      <li v-for="todo of todos" :key='todo.id' :class="{done: todo.done}">
        <span>{{todo.id}} == {{todo.title}}</span>
      </li>
    </ul>
  </div>
  <script>
    new Vue({
      el: '#app',
      data() {
        return {
          todos: [],
          newVal: ''
        }
      },
      created() {
        this.getTodos()
      },
      methods: {
        getTodos() {
          fetch('/todos').then(res => {
          return res.json()
        }).then(data => {
          this.todos = data.data
        })
        },
        add() {
          fetch('/add', {
            method: 'post',
            headers: {
              'content-type': 'application/json'
            },
            body: JSON.stringify({title: this.newVal})
          }).then(res => {
            return res.json()
          }).then(data => {
            // this.todos = data.data
            this.getTodos()
          })
          this.newVal = ''
        }
      }
    })
  </script>
</body>
</html>